<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

// Event::listen('illuminate.query', function($sql,$param) {
//     file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sql.log',$sql.'['.print_r($param, 1).']'."\r\n",8);
// });

Event::listen('illuminate.query', function($sql, $param) {
    file_put_contents(public_path().'/sql.log',$sql.'['.printr($param, 1).']'."\r\n",8);
//echo sql.",with[".join(′,′,param) .”]“;
});

Route::get('doc', 'Api\SwaggerController@doc');

//Route::get('register', function () {
//    $user = new App\Models\User();
//    $user->name = "tester";
//    $user->email = "test@test.com";
//    $user->password = \Illuminate\Support\Facades\Hash::make("password");
//    $user->save();
//});

Route::post('oauth/token', '\Laravel\Passport\Http\Controllers\AccessTokenController@issueToken');
Route::post('user/register', 'Auth\RegisterController@register');

// 返回 user_id，上面的 'oauth/token' 接口是扩展返回的数据，不宜在里面修改。
Route::get('oauth/userid', 'Api\V1\UserController@getUserId');


// 陈玉琦 测试专用
// Route::get('test', 'Api\V1\TestController@index');
// Route::get('test/test', 'Api\V1\TestController@test');
// Route::get('activity', 'Api\V1\ActivityController@index');
// Route::get('activity', 'Api\V1\ActivityController@index');

// 普通用户报名参加系统用户发布的活动（参与者，无需登录，因此无需认证）
Route::post('participant', 'Api\V1\ParticipantController@store');
Route::get('activity/scoreTop', 'Api\V1\ActivityController@scoreTop');

// 中奖
Route::post('award/hitAward', 'Api\V1\AwardSetController@hitAward');
// 奖品设置情况
Route::get('award', 'Api\V1\AwardSetController@index');
// 所有人中奖情况
Route::get('award/hitAwards', 'Api\V1\AwardSetController@getHitAwards');

// 下载（这些应该登录后才能下载吧）
Route::get('activity/download', 'Api\V1\ActivityController@download');
Route::get('award/download', 'Api\V1\AwardSetController@download');
Route::get('vote/download', 'Api\V1\VoteController@download');
Route::get('questionnaire/download', 'Api\V1\QuestionnaireController@download');
Route::get('activity/downloadExample', 'Api\V1\ActivityController@downloadExample');

// 结果反馈（参与者，无需登录，因此无需认证）
Route::post('vote/reply', 'Api\V1\VoteController@reply');
Route::post('questionnaire/reply', 'Api\V1\QuestionnaireController@reply');
Route::post('activity/reply', 'Api\V1\ActivityController@reply');

Route::get('vote', 'Api\V1\VoteController@index');
Route::get('questionnaire', 'Api\V1\QuestionnaireController@index');


// 必须放在 'play/{id}' 这种路由的前面，否则 'play/animation' 的请求会被 'play/{id}' 接收处理；
Route::get('play/animation', 'Api\V1\PlayController@animation');
// 陈玉琦++ 任何人都可以看到，不需要授权
// 下面的 play => resource 资源路由中，except 添加 show。
// 这两处都是必须的，否则失败。
Route::get('play/{id}', 'Api\V1\PlayController@show');
Route::get('play/count/{id}', 'Api\V1\PlayController@count');// 访问量累加
// Route::get('play', 'Api\V1\PlayController@index');// postman 测试使用 -- 注意在下面排除 index；
// Route::post('play/store', 'Api\V1\PlayController@store');// postman 测试使用 -- 注意在下面排除 store；


$api = app('Dingo\Api\Routing\Router');

$api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($api) {

    $api->resource('extend', 'ExtendController', ['only' => ['index']]);

    $api->resource('template', 'TemplateController', ['only' => ['index', 'show']]);
    
    $api->group(['middleware' => 'api:auth'], function ($api) {
    //$api->group([], function ($api) {
        $api->get('user/info', 'UserController@getInfo');

        $api->delete('image', 'ImageController@destroy');
        $api->get('image/public', 'ImageController@public_images');
        $api->resource('image', 'ImageController', ['only' => ['index', 'store']]);

        $api->delete('audio', 'AudioController@destroy');
        $api->get('audio/public', 'AudioController@public_audios');
        $api->resource('audio', 'AudioController', ['only' => ['index', 'store']]);

        $api->post('play/update_play', 'PlayController@update_play');
        $api->post('play/destroy_play', 'PlayController@destroy_play');
        // $api->get('play/animation', 'PlayController@animation');
        $api->resource('play', 'PlayController', ['except' => ['create', 'edit', 'update', 'destroy', 'show', 'animation']]);

        $api->resource('draft', 'DraftController', ['only' => ['index', 'store']]);

        $api->resource('form', 'FromController', ['only' => ['store']]);

        $api->delete('music', 'MusicController@destroy');
        $api->get('music/public', 'MusicController@public_images');
        $api->resource('music', 'MusicController', ['only' => ['index', 'store']]);

        // 活动参与者
        // 根据活动 activity_id 查询参与者的信息
        $api->get('participant/activity/{activity_id}', 'ParticipantController@getByActivityId');
        // 根据 user_id 查询参与者的信息（user_id 是系统用户，即发布活动的用户 id；participant_id 是参与活动的用户 id）
        $api->get('participant/activity/user/{user_id}', 'ParticipantController@getActivityParticipantsByUserId');
        // 根据调查 survey_id 查询参与者的信息
        $api->get('participant/survey/{survey_id}', 'ParticipantController@getBySurveyId');
        // 根据 user_id 查询参与者的信息（user_id 是系统用户，即发布调查的用户 id；participant_id 是参与调查的用户 id）
        $api->get('participant/survey/user/{user_id}', 'ParticipantController@getSurveyParticipantsByUserId');
        // 用户发布的所有活动和调查的参与者信息
        $api->get('participant/user/{user_id}', 'ParticipantController@getParticipantsByUserId');


        // 资源路由
        // php artisan make:controller Admin\ArticleController  --resource
        // $api->resource('activity', 'ActivityController', ['except' => ['create', 'edit', 'download']]);
        // 没有定义 get: domain/api/activity/{user_id}，无法使用 public function index($user_id){} 来接收，也无法使用 $request->input('user_id') 来接收。
        $api->get('activity', 'ActivityController@index');
        $api->post('activity', 'ActivityController@store');
        $api->put('activity/{id}', 'ActivityController@update');
        $api->delete('activity/{id}', 'ActivityController@destroy');

        // 投票
        $api->post('vote', 'VoteController@store');
        $api->put('vote/{id}', 'VoteController@update');
        $api->delete('vote/{id}', 'VoteController@destroy');

        // 问卷
        $api->post('questionnaire', 'QuestionnaireController@store');
        $api->put('questionnaire/{id}', 'QuestionnaireController@update');
        $api->delete('questionnaire/{id}', 'QuestionnaireController@destroy');

        // 大屏幕
        $api->resource('screen', 'ScreenController', ['except' => ['create', 'edit']]);
    });
});
